#include "GameSprite.h"

GameSprite::GameSprite()
{
	m_pBody = NULL;
	m_eStatus = EGSS_move;
}

GameSprite::~GameSprite()
{
	m_pBody = NULL;
}

//GameSprite* GameSprite::create(const char* name, b2World* world)
//{
//	GameSprite* p = new GameSprite();
//	if (p && p->init(name))
//	{
//		p->CreateBody(world);
//		p->autorelease();
//		return p;
//	}
//
//	CC_SAFE_DELETE(p);
//	return p;
//}

b2Body* GameSprite::CreateBody(b2World* world)
{
	b2BodyDef bodyDef;
	bodyDef.position.Set(0, 2.1f);
	bodyDef.type = b2_dynamicBody;
	bodyDef.gravityScale = 0;
	
	m_pBody = world->CreateBody(&bodyDef);

	b2FixtureDef fd;
	fd.restitution = 0;
	fd.density = 1;
	fd.filter.categoryBits = ECM_Enemy;
	fd.filter.maskBits = ECM_Group | ECM_Hero;
	b2PolygonShape share;
	share.SetAsBox(0.5, 0.5);
	fd.shape = &share;
	m_pBody->CreateFixture(&fd);
	m_pBody->SetUserData( new MyUserData(this));
	return m_pBody;
}

void GameSprite::setPosition(const CCPoint &position)
{
	if (m_pBody == nullptr) { return; }
	b2Vec2 pos(position.x / PTM_RATIO, position.y / PTM_RATIO);
	m_pBody->SetTransform(pos, m_pBody->GetAngle());
	CCArmature::setPosition(position);
}

void GameSprite::onPhyPosChange()
{
	b2Vec2 pos = m_pBody->GetPosition();
	CCPoint pt = ccp(pos.x * PTM_RATIO, pos.y * PTM_RATIO);
	CCArmature::setPosition(pt);
}

